package com.easidea.tim.flex.remote.supply
{
	import com.easidea.tim.flex.Env;
	import com.easidea.tim.flex.bean.Page;
	import com.easidea.tim.flex.bean.supply.SupType;
	import com.easidea.tim.flex.bean.supply.SupplierLocator;
	import com.easidea.tim.flex.bean.supply.SupplierTypeDTO;
	import com.easidea.tim.flex.event.CustomerEvent;
	import com.easidea.tim.flex.remote.BaseDao;
	import com.easidea.tim.flex.util.AppAlert;
	
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.remoting.RemoteObject;
	
	public class SupplierTypeDao extends BaseDao 
	{
		private var _supplierTypeService:RemoteObject = new RemoteObject();
		[Bindable]private var supplierLocator:SupplierLocator = SupplierLocator.getInstance();
		private static var _instance:SupplierTypeDao;
		
		public static function getInstance():SupplierTypeDao{
			if( !_instance ){
				_instance = new SupplierTypeDao();
				
			}
			return _instance;
		}
		public function SupplierTypeDao() {
			_supplierTypeService.channelSet = this.buildChannelSet();
			_supplierTypeService.destination="supTypeService";	
		}
		/**
		 * 调用spring方法:根据供应商类别名称等参数分页查询供应商类别列表
		 * @param name
		 * @param page
		 * 
		 */		
		public function getSupplierTypeList(name:String,page:Page):void {			
			_supplierTypeService.addEventListener(ResultEvent.RESULT, showSupplierTypeList);
			_supplierTypeService.addEventListener(FaultEvent.FAULT,_Fault);
			_supplierTypeService.getSupplierTypeListByContion(name,page);
			
		}
		/**
		 * 根据供应商类别名称等参数分页查询供应商类别列表回调函数
		 * @param e
		 * 
		 */			
		private function showSupplierTypeList(e:ResultEvent):void {			 
			_supplierTypeService.removeEventListener(ResultEvent.RESULT, showSupplierTypeList);
			 if(e.result != null) {		
				var page:Page = e.result as Page;					 				 
				Env.dispatchEvent(new CustomerEvent("getSupplierTypeListSuccess",page));				  									 
			 } 
		}

		/**
		 *调用spring方法:新增或编辑供应商类别保存 
		 * @param customer
		 * 
		 */		
		public function saveSupplierType(supplierType:SupType):void {
	
				_supplierTypeService.addEventListener(ResultEvent.RESULT, saveSupplierTypeSuccess);
				_supplierTypeService.addEventListener(FaultEvent.FAULT,_Fault);
				_supplierTypeService.saveOrUpdateSupplierType(supplierType);
		}
	   /**
	    *新增或编辑供应商类别保存回调函数 
	    * @param e
	    * 
	    */		
	   private function saveSupplierTypeSuccess(e:ResultEvent):void {			 
			_supplierTypeService.removeEventListener(ResultEvent.RESULT, saveSupplierTypeSuccess);
			 if(e.result != null) {
				AppAlert.show("保存成功！","提示");
				Env.dispatchEvent(new CustomerEvent("saveSupplierTypeSuccess"));
			 } 
		}
		/**
		 *调用spring方法:删除供应商类别
		 * @param customer
		 * 
		 */		
		public function deleteSupplierType(id:String):void {
	
				_supplierTypeService.addEventListener(ResultEvent.RESULT, deleteSupplierTypeSuccess);
				_supplierTypeService.addEventListener(FaultEvent.FAULT,_Fault);
				_supplierTypeService.deleteSupplierType(id);
		}
	   /**
	    * 删除供应商类别回调函数 
	    * @param e
	    * 
	    */		
	   private function deleteSupplierTypeSuccess(e:ResultEvent):void {			 
			_supplierTypeService.removeEventListener(ResultEvent.RESULT, deleteSupplierTypeSuccess);
			 if(e.result) {
				AppAlert.show("删除成功！","提示");
				Env.dispatchEvent(new CustomerEvent("deleteSupplierTypeSuccess"));
			 } else{
			 	AppAlert.show("该类别下存在供应商信息，无法删除！","提示");
				Env.dispatchEvent(new CustomerEvent("deleteSupplierTypeSuccess"));
			 }
		}
		
	}

	
}